package com.mango.coomb.threads;


import com.mango.coomb.constant.ThreadConst;
import com.mango.coomb.enums.AreaClass;
import com.mango.coomb.enums.Status;
import com.mango.coomb.enums.ThreadClass;
import com.mango.coomb.threads.parameter.ThreadParameter;
import com.mango.coomb.threads.parameter.util.ThreadParamUpdateUtil;

public class WriterWrite extends Thread {
    private static final Integer[] writerBufferValueArr = ThreadParameter.getWriterBufferValueArr();
    private static final Integer[] bufferBufferValueArr = ThreadParameter.getBufferBufferValueArr();
    private Status status = Status.NOT_BEGIN;
    private boolean paused=false;

    @Override
    public void run() {
        while (ThreadParameter.isWriterWriteRunning) {
            while (!this.paused) {
                int j = ThreadParamUpdateUtil.searchNull(AreaClass.BUFFER, null);
                if (j == ThreadConst.NOT_SEARCH) {
                    this.status = Status.PAUSED;
                    try {
                        synchronized (bufferBufferValueArr) {
                            bufferBufferValueArr.wait();
                        }
                    } catch (InterruptedException e) {
                        throw new RuntimeException(e);
                    }
                } else {
                    Integer value = null;
                    int i = ThreadParamUpdateUtil.searchNotNull(AreaClass.WRITER, null);
                    if (i == ThreadConst.NOT_SEARCH) {
                        ThreadParameter.isBufferBufferLockedArr[j] = false;
                        this.status = Status.PAUSED;
                        try {
                            synchronized (writerBufferValueArr) {
                                writerBufferValueArr.wait();
                            }
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        continue;
                    } else {
                        this.status = Status.PAUSED;
                        synchronized (ThreadParameter.writerBufferLock[i]) {
                            this.status = Status.RUNNING;
//                            ThreadParameter.isWriterBufferLockedArr[i] = true;
                            try {
                                Thread.sleep(1500L / ThreadParameter.getSpeedMultiple(ThreadClass.WRITER));
                            } catch (InterruptedException e) {
                                throw new RuntimeException(e);
                            }
                            value = writerBufferValueArr[i];
                            ThreadParamUpdateUtil.updateWriterBufferValueArr(i, null);
                            ThreadParameter.isWriterBufferLockedArr[i] = false;
                            this.status = Status.NOT_BEGIN;
                        }
                        synchronized (writerBufferValueArr) {
                            writerBufferValueArr.notifyAll();
                        }
                    }
                    this.status = Status.PAUSED;
                    synchronized (ThreadParameter.bufferBufferLock[j]) {
                        this.status = Status.RUNNING;
//                        ThreadParameter.isBufferBufferLockedArr[j] = true;
                        try {
                            Thread.sleep(1500L / ThreadParameter.getSpeedMultiple(ThreadClass.WRITER));
                        } catch (InterruptedException e) {
                            throw new RuntimeException(e);
                        }
                        ThreadParamUpdateUtil.updateBufferBufferValueArr(j, value);
                        ThreadParameter.isBufferBufferLockedArr[j] = false;
                        this.status = Status.NOT_BEGIN;
                    }
//                    synchronized (readerPosBufferValueArr) {
//                        readerPosBufferValueArr.notifyAll();
//                    }
//                    synchronized (readerNegBufferValueArr) {
//                        readerNegBufferValueArr.notifyAll();
//                    }
//                    synchronized (readerZeroBufferValueArr) {
//                        readerZeroBufferValueArr.notifyAll();
//                    }
                    synchronized (bufferBufferValueArr) {
                        bufferBufferValueArr.notifyAll();
                    }
                }
            }
//            this.status = Status.PAUSED;
            try {
                synchronized (ThreadParameter.writerWriteLock) {
                    ThreadParameter.writerWriteLock.wait();
                }
//                synchronized (ThreadParameter.totalLock){
//                    ThreadParameter.totalLock.wait();
//                }
            } catch (InterruptedException e) {
                throw new RuntimeException(e);
            }
////            Thread.interrupted();
//            this.status = Status.NOT_BEGIN;
        }

    }

    public Status getStatus() {
        return status;
    }

    public void setStatus(Status status) {
        this.status = status;
    }

    public boolean isPaused() {
        return paused;
    }

    public void setPaused(boolean paused) {
        this.paused = paused;
    }
}